﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.Objects;

namespace CCF20Model.ModelBase
{
    public class DropDownItem
    {
        public string name { get; set; }
        public int value { get; set; }
        public override string ToString()
        {
            return name;
        }
    }

    public class ModelBaseClass
    {
        public CCF20Entities ccf20Ent { get; set; }
        protected CCF20Entities ccf20Entities;
        protected static string CurrentUser = string.Empty;
        protected static string CurrentUserMobile = string.Empty;
        public ModelBaseClass(CCF20Entities ccf20)
        {
            
            ccf20Ent = ccf20;
            ccf20Entities = ccf20;
            try
            {
                CurrentUser = (string)System.Windows.Application.Current.Properties["CurrentUser"];
                CurrentUserMobile = (string)System.Windows.Application.Current.Properties["CurrentUserMobile"];
            }catch
            {
                
            }
            if ( (string.IsNullOrEmpty(CurrentUser)) || string.IsNullOrEmpty(CurrentUserMobile))
            {
                OnCall onCall = ProfileModel.GetOnCallInfo();
                CurrentUser = onCall.Name;
                CurrentUserMobile = onCall.Mobile;
                //LoggerHelper.LogCCF20(string.Format("值班人员姓名={0} 电话={1}", CurrentUser, CurrentUserMobile));
            }
            ccf20Ent.ObjectMaterialized += new System.Data.Objects.ObjectMaterializedEventHandler(ccf20Ent_ObjectMaterialized);
        }
        public ModelBaseClass()
        {
        }
        void ccf20Ent_ObjectMaterialized(object sender, System.Data.Objects.ObjectMaterializedEventArgs e)
        {
            if (e.Entity is Customer)
            {
                Customer customer = (Customer)e.Entity;
                customer.isObjectMaterialzied = true;
            }
            var entity = e.Entity as IEntityWithContext;
            if (entity != null)
            {
                entity.Context = ccf20Ent;
            }

            
        }

        protected void RefreshQuery(IEnumerable query)
        {

            var failedEntities = from p in ccf20Entities.ObjectStateManager.GetObjectStateEntries()
                                 where p.Entity != null && p.EntityKey != null && p.State != EntityState.Added
                                 select p.Entity;

            ccf20Entities.Refresh(System.Data.Objects.RefreshMode.StoreWins, failedEntities);

        }
        public void Update()
        {
            try
            {
                ccf20Entities.SaveChanges();
            }
            catch (OptimisticConcurrencyException ex)
            {
                Utility.ShowMessageBox("该条记录已经被他人更新，请刷新数据后在操作");
                var conflictEntity = ex.StateEntries[0].Entity;
                LoggerHelper.LogCCF20(string.Format("database conflict detected, refresh {0} with StoreWin", conflictEntity.GetType().Name));
                ccf20Entities.Refresh(RefreshMode.StoreWins, conflictEntity);
                
            }
        }
        
        public IEnumerable<string> GetVehicleBrand()
        {
            var query = (from v in ccf20Ent.VehicleTypes
                         select v.Manufacture).Distinct();
            return query;
        }

        public IEnumerable<string> GetVehicleModelByBrand(string brand)
        {
            var query = (from v in ccf20Ent.VehicleTypes
                         where v.Manufacture.Equals(brand)
                         select v.Model).Distinct();
            return query;
        }

        public IEnumerable<DropDownItem> GetVehicleEngineByBrandModel(string brand, string model)
        {
            var query = from v in ccf20Ent.VehicleTypes
                        where v.Manufacture.Equals(brand) && v.Model.Equals(model)
                        select new DropDownItem
                        {
                            name = v.EngineType + "-" + v.ModelYear,
                            value = v.VehicleTypeID
                        };

            return query;
        }

        public IEnumerable<DropDownItem> GetChinaProvince()
        {
            var query = from v in ccf20Ent.ChinaProvinces
                        select new DropDownItem
                        {
                            name = v.Province,
                            value = v.ProvinceID
                        };
            return query;
        }

        public IEnumerable<DropDownItem> GetChinaCityByProvince(int provinceID)
        {
            var query = from v in ccf20Ent.ChinaCities
                        where v.ProvinceID == provinceID
                        select new DropDownItem
                        {
                            name = v.City,
                            value = v.CityID
                        };
            return query;
        }

        public IEnumerable<DropDownItem> GetIDSByCity(string city)
        {
            var query = from v in ccf20Ent.WiseLinkIDS
                        where v.CompanyAddress.Contains(city)
                        select new DropDownItem
                        {
                            name = v.CompanyName,
                            value = (int)v.IDS
                        };
            return query;
        }
        public IEnumerable<DropDownItem> GetIDSByCity()
        {
            var query = from v in ccf20Ent.WiseLinkIDS
                        select new DropDownItem
                        {
                            name = v.CompanyName,
                            value = (int)v.IDS
                        };
            return query;
        }
        public VehicleType GetVehicleTypeById(int id)
        {
            return ccf20Ent.VehicleTypes.Where(v => v.VehicleTypeID == id).FirstOrDefault();
        }

    }
}
